package utils

import (
	"strconv"
	"strings"
	"time"
)

func FormatTime(now time.Time, format string) string {
	return now.Format(format)
}

func GetCurrentDayInt() int64 {
	cur, _ := strconv.ParseInt(FormatTime(time.Now(), "20060102"), 10, 64)
	return cur
}
func GetDayStr() string {
	return FormatTime(time.Now(), "2006-01-02")
}

func Str2Date(valOrg string) int64 {

	if valOrg == "" {
		return 0
	}
	value := strings.ReplaceAll(strings.TrimSpace(valOrg), "\\", "")
	flag := false
	formatStr := "2006-01-02"
	if strings.Contains(value, "/") {
		formatStr = "2006/01/02"
		flag = true
	} else if strings.Contains(value, "年") {
		formatStr = "2006年01月02日"
		flag = true
	} else if strings.Contains(value, "-") {
		flag = true
	}
	if flag {
		time1, err := time.ParseInLocation(formatStr, value, time.Local)
		if err != nil {
			return 0
		} else {
			return time1.Unix()
		}
	} else {
		return excelDateToDate(value).Unix()
	}
}
func excelDateToDate(excelDate string) time.Time {
	excelTime := time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)
	var days, _ = strconv.Atoi(excelDate)
	return excelTime.Add(time.Second * time.Duration(days*86400))
}
